package org.dreamwork.persistence;

import org.dreamwork.util.IDataCollection;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-8-25
 * Time: 13:03:18
 */
public class GenericServiceSpringImpl<T, PK extends Serializable> implements IGenericService<T, PK> {
    protected IGenericDao<T, PK> dao;

    public IGenericDao<T, PK> getDao () {
        return dao;
    }

    public void setDao (IGenericDao<T, PK> dao) {
        this.dao = dao;
    }

    public PK save (T o) {
        return dao.save (o);
    }

    public void save (T... o) {
        dao.save (o);
    }

    public void save (Collection<T> o) {
        dao.save (o);
    }

    public void update (T o) {
        dao.update (o);
    }

    public void update (T... o) {
        dao.update (o);
    }

    public void update (Collection<T> o) {
        dao.update (o);
    }

    public void delete (T o) {
        dao.delete (o);
    }

    public void delete (T... o) {
        dao.delete (o);
    }

    public void delete (Collection<T> o) {
        dao.delete (o);
    }

    public T get (PK id) {
        return dao.get (id);
    }

    public T get (Parameter... parameters) {
        return dao.get (parameters);
    }

    public T get (Collection<Parameter> parameters) {
        return get (toParameterArray(parameters));
    }

    public T get (Sort sort, Parameter... parameters) {
        return dao.get (sort, parameters);
    }

    public T get (Sort sort, Collection<Parameter> parameters) {
        return dao.get (sort, parameters);
    }

    public boolean exists (PK id) {
        return dao.exists (id);
    }

    public boolean exists (Parameter... parameters) {
        return dao.exists (parameters);
    }

    public boolean exists (Collection<Parameter> parameters) {
        return exists (toParameterArray(parameters));
    }

    public List<T> find (Sort... sort) {
        return find (Arrays.asList (sort));
    }

    public List<T> find (List<Sort> sort) {
        return dao.find (sort);
    }

    public List<T> find (Parameter... parameters) {
        return dao.find (parameters);
    }

    public List<T> find (Collection<Parameter> parameters) {
        return find (toParameterArray(parameters));
    }

    public List<T> find (Collection<Sort> s, Parameter... p) {
        return dao.find (s, p);
    }

    public List<T> find (Collection<Sort> s, Collection<Parameter> p) {
        return dao.find (s, p);
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Parameter... p) {
        return dao.find (pageNo, pageSize, p);
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Collection<Parameter> p) {
        return dao.find (pageNo, pageSize, p);
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Sort sort, Parameter... parameters) {
        return dao.find (pageNo, pageSize, sort, parameters);
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Sort sort, Collection<Parameter> parameters) {
        return find (pageNo, pageSize, sort, toParameterArray(parameters));
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Collection<Sort> s, Parameter... p) {
        return dao.find (pageNo, pageSize, s, p);
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Collection<Sort> s, Collection<Parameter> p) {
        return dao.find (pageNo, pageSize, s, p);
    }

    public int executeUpdate (String hql, Parameter... parameters) {
        return dao.executeUpdate (hql, parameters);
    }

    public int executeUpdate (String hql, Collection<Parameter> parameters) {
        return dao.executeUpdate (hql, parameters);
    }

    public List executeQuery (String hql, Parameter... parameters) {
        return dao.executeQuery (hql, parameters);
    }

    public List executeQuery (String hql, Collection<Parameter> parameters) {
        return dao.executeQuery (hql, parameters);
    }

    public IDataCollection executeQuery (int pageNo, int pageSize, String hql, Parameter... parameters) {
        return dao.executeQuery (pageNo, pageSize, hql, parameters);
    }

    public IDataCollection executeQuery (boolean flag, int pageNo, int pageSize, String hql, Parameter... parameters) {
        return dao.executeQuery (flag, pageNo, pageSize, hql, parameters);
    }

    public IDataCollection executeQuery (int pageNo, int pageSize, String hql, Collection<Parameter> parameters) {
        return dao.executeQuery (pageNo, pageSize, hql, parameters);
    }

    public IDataCollection executeQuery (boolean flag, int pageNo, int pageSize, String hql, Collection<Parameter> parameters) {
        Parameter[] ps = new Parameter[parameters.size ()];
        ps = parameters.toArray (ps);
        return dao.executeQuery (flag, pageNo, pageSize, hql, ps);
    }

    public int executeNativeUpdate (String sql, Parameter... parameters) {
        return dao.executeNativeUpdate (sql, parameters);
    }

    public int executeNativeUpdate (String sql, Collection<Parameter> parameters) {
        return dao.executeNativeUpdate (sql, parameters);
    }

    public List<Map<String, Object>> executeNativeQuery (String sql, Parameter... parameters) {
        return dao.executeNativeQuery (sql, parameters);
    }

    public List<Map<String, Object>> executeNativeQuery (String sql, Collection<Parameter> parameters) {
        return dao.executeNativeQuery (sql, parameters);
    }

    public IDataCollection<Map<String, Object>> executeNativeQuery (int pageNo, int pageSize, String sql, Parameter... parameters) {
        return dao.executeNativeQuery (pageNo, pageSize, sql, parameters);
    }

    public IDataCollection<Map<String, Object>> executeNativeQuery (int pageNo, int pageSize, String sql, Collection<Parameter> parameters) {
        return dao.executeNativeQuery (pageNo, pageSize, sql, parameters);
    }

    public <C> C get (Class<C> type, Serializable pk) {
        return dao.get (type, pk);
    }

    public <C> void save (Class<C> type, C... instances) {
        dao.save (type, instances);
    }

    public <C> void save (Class<C> type, Collection<C> instances) {
        dao.save (type, instances);
    }

    public <C> void update (Class<C> type, C... instances) {
        dao.update (type, instances);
    }

    public <C> void update (Class<C> type, Collection<C> instances) {
        dao.update (type, instances);
    }

/*
    public <C> C get (Class<C> type, Serializable pk) {
        return dao.get (type, pk);
    }
*/

    public Number executeScale (String hql, Collection<Parameter> parameters) {
        return dao.executeScale (hql, parameters);
    }

    public Number executeScale (String hql, Parameter... parameters) {
        return dao.executeScale (hql, parameters);
    }

    public Number executeNativeScale (String sql, Collection<Parameter> parameters) {
        return dao.executeNativeScale (sql, parameters);
    }

    public Number executeNativeScale (String sql, Parameter... parameters) {
        return dao.executeNativeScale (sql, parameters);
    }

    protected Parameter[] toParameterArray (Collection<Parameter> coll) {
        Parameter[] array = new Parameter[coll.size ()];
        return coll.toArray (array);
    }

    @SuppressWarnings ("unchecked")
    protected<C> C get (String hql, Parameter... parameters) {
        List<C> list = dao.executeQuery (hql, parameters);
        return (list == null || list.isEmpty ()) ? null : list.get (0);
    }

    protected Map<String, Object> nativeGet (String sql, Parameter... parameters) {
        List<Map<String, Object>> list = dao.executeNativeQuery (sql, parameters);
        return (list == null || list.isEmpty ()) ? null : list.get (0);
    }
}